package com.example.project3.controller;

import com.example.project3.entity.Projects;
import com.example.project3.entity.Stages;
import com.example.project3.exception.ServiceException;
import com.example.project3.service.impl.ProjectsServiceImpl;
import com.example.project3.service.impl.StagesServiceImpl;
import com.example.project3.util.API;
import com.example.project3.util.MyJWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName StageController .java
 * @Description empty
 * @Author LiuJinyu
 * @Date 2023/5/31 16:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/stage")
public class StageController {
    private MyJWT myJWT;
    private ProjectsServiceImpl projectsServiceImpl;
    private StagesServiceImpl stagesServiceImpl;


    @PostMapping("/new")
    public API createStage(@RequestBody Stages stage, HttpServletRequest httpServletRequest) {
        Projects project = projectsServiceImpl.getById(stage.getProjectId());
        if (Objects.equals(project.getMasterId(), myJWT.getUid(httpServletRequest))) {
            stagesServiceImpl.save(stage);
        } else {
            throw new ServiceException("权限不足");
        }
        return API.success(stage);
    }

    @DeleteMapping("/del")
    public API del(@RequestBody Stages stage, HttpServletRequest request) {
        stage = stagesServiceImpl.getById(stage.getStageId());
        Projects project = projectsServiceImpl.getById(stage.getProjectId());
        if (Objects.equals(myJWT.getUid(request), project.getMasterId())) {
            stagesServiceImpl.removeById(stage.getStageId());
        } else {
            throw new ServiceException("权限不足");
        }
        return API.success();
    }

    @PostMapping("/update")
    public API update(HttpServletRequest request, @RequestBody Stages stage) {
        stage = stagesServiceImpl.getById(stage.getStageId());
        Projects project = projectsServiceImpl.getById(stage.getProjectId());
        if (Objects.equals(myJWT.getUid(request), project.getMasterId())) {
            stagesServiceImpl.updateById(stage);
        } else {
            throw new ServiceException("权限不足");
        }
        return API.success();
    }

    @GetMapping("/get")
    public API getStagesByPId(@RequestParam Integer projectId) {
        Map<String, Object> query = new HashMap<>();
        query.put("project_id", projectId);
        List<Stages> list = stagesServiceImpl.listByMap(query);
        return API.success(list);
    }

    @Autowired
    public void setMyJWT(MyJWT myJWT) {
        this.myJWT = myJWT;
    }

    @Autowired
    public void setProjectsServiceImpl(ProjectsServiceImpl projectsServiceImpl) {
        this.projectsServiceImpl = projectsServiceImpl;
    }

    @Autowired
    public void setStagesServiceImpl(StagesServiceImpl stagesServiceImpl) {
        this.stagesServiceImpl = stagesServiceImpl;
    }
}
